/* eslint-disable no-extend-native */

Array.prototype.remove = function (val) {
  for (let i = 0; i < this.length; ) {
    if (this[i] === val) {
      this.splice(i, 1);
    } else {
      i++;
    }
  }
};

Array.prototype.unique = function () {
  let r = [];
  for (let o of this) {
    if (!r.includes(o)) {
      r.push(o);
    }
  }
  return r;
};

Array.prototype.replace = function (target, value) {
  const index = this.indexOf(target);
  if (index !== -1) {
    this[index] = value;
  }
};

Array.prototype.last = function () {
  return this[this.length - 1];
};

Array.prototype.differ = function (target) {
  return this.filter((e) => {
    return !target.includes(e);
  });
};

Array.prototype.insert = function (target) {
  target.forEach((e) => {
    if (!this.includes(e)) this.push(e);
  });
};

Array.prototype.matrix = function (size) {
  const result = [];
  for (let x = 0; x < Math.ceil(this.length / size); x++) {
    let start = x * size;
    let end = start + size;
    result.push(this.slice(start, end));
  }
  return result;
};
